package com.example.administrator.dc.helper;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.example.administrator.dc.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * author hation
 * time 2017/8/15  21:10
 */
public class Carousel {
    /**
     *
     * @param context 该activity 的context
     * @param convenient 轮播图控件
     * @param images 图片集合
     * @param width 轮播图宽度
     * @param height 轮播图高度
     */
    public static void setCarousel(final Context context, ConvenientBanner convenient, List<String> images, final int width, final int height) {
        convenient.startTurning(3000);
        convenient.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public Carousel.NetworkImageHolderView createHolder() {
                return new Carousel.NetworkImageHolderView(context,width,height);
            }
        },images)
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }
    private static class NetworkImageHolderView  implements Holder<String> {
        private ImageView imageView;
        private Context c;
        private int width,height;
        public NetworkImageHolderView(Context cc,int width,int height){
            this.c=cc;
            this.width=width;
            this.height=height;
        }
        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            return imageView;
        }
        @Override
        public void UpdateUI(Context context, int position, String data) {
           Picasso.with(context).load(data).resize(width,height).into(imageView);
           // Glide.with(c).load(data).into(imageView);
        }
    }
}
